
namespace OpenComposite.EII.RepositoryUIs
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Diagnostics;
	using System.Drawing;
	using System.IO;
	using System.Runtime.Serialization;
	using System.ServiceModel.Description;
	using System.Windows.Forms;
	using System.Xml;
	using System.Xml.Schema;
	using OpenComposite.Base.Collections;
	using OpenComposite.EII.Forms;
	using OpenComposite.EII.Repository;
	using OpenComposite.Mapper;
	using OpenComposite.Repository.Properties;

	public partial class UCLogicalWebMethod : RepositoryItemUI
	{
		#region Initialization

		/// <summary>
		/// Initializes a new instance of the <see cref="UCLogicalWebMethod"/> class.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		public UCLogicalWebMethod(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets or sets the repository item.
		/// </summary>
		/// <value>The repository item.</value>
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _logwmeth;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is LogicalWebMethod ) {
					_logwmeth = (LogicalWebMethod)value;
					load();
				}
			}
		}

		/// <summary>
		/// Gets the type description.
		/// </summary>
		/// <value>The type description.</value>
		public override string TypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the type text.
		/// </summary>
		/// <value>The type text.</value>
		public override string TypeText
		{
			get { return Resources.sLogicalWebMethod; }
		}

		#endregion Properties

		#endregion Public Members

		#region Protected Members

		#region Methods

		/// <summary>
		/// Notifies the property change.
		/// </summary>
		/// <param name="property">The property.</param>
		/// <param name="value">The value.</param>
		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtVerbName.Text = value.ToString();
			}
		}

		#endregion Methods

		#endregion Protected Members

		#region Private Members

		#region Fields

		private RepositoryBusinessRule _endpointRule = null;
		private LogicalWebMethod _logwmeth = null;
		private MessageQueueEx _receiveMessageQueue = null;
		private Dictionary<string, LogicalRuleResultEndpoint> _ruleResultEndpoints = null;
		private MessageQueueEx _sendMessageQueue = null;
		private WebService _wsEP = null;
		private WebService _wsEPProxy = null;
		private WebService _wsProxy = null;

		#endregion Fields

		#region Event Handlers

		private void UCLogicalWebMethod_Load(object sender, EventArgs e)
		{
		}

		private void btnEPInputMap_Click(object sender, EventArgs e)
		{
			doMapping(true, false);
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			picEPInputIsMapped.Image =
				( lrre.InputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnEPInputView_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			WebMethod wm = lrre.WebMethod;
			if ( wm != null && wm.InputSchema != null ) {
				showSchema(wm.InputSchema, "Input Contract for Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnEPOutputMap_Click(object sender, EventArgs e)
		{
			doMapping(false, false);
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			picEPOutputIsMapped.Image =
				( lrre.OutputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnEPOutputView_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			WebMethod wm = lrre.WebMethod;
			if ( wm != null && wm.OutputSchema != null ) {
				showSchema(wm.OutputSchema, "Output Contract for Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnEPProxyInMap_Click(object sender, EventArgs e)
		{
			doMapping(true, true);
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			picEPProxyInIsMapped.Image =
				( lrre.ProxyInputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnEPProxyInView_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			WebMethod wm = lrre.ProxyWebMethod;
			if ( wm != null && wm.InputSchema != null ) {
				showSchema(wm.InputSchema, "Input Contract for Proxy Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnEPProxyOutMap_Click(object sender, EventArgs e)
		{
			doMapping(false, true);
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			picEPProxyOutIsMapped.Image =
				( lrre.ProxyOutputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnEPProxyOutView_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;
			WebMethod wm = lrre.ProxyWebMethod;
			if ( wm != null && wm.OutputSchema != null ) {
				showSchema(wm.OutputSchema, "Output Contract for Proxy Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnEPProxySelectWS_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( _wsEPProxy == null ) _wsEPProxy = lrre.ProxyWebService;

			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_logwmeth);
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.AllowedTypes = new ItemType[] { ItemType.WebService };
			frm.MultiSelect = false;
			frm.SelectedItem = _wsEPProxy;
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem is WebService ) {
				this.Refresh();
				_wsEPProxy = frm.SelectedItem as WebService;
				showWebServiceMethod(true, true);
			}
		}

		private void btnEPSelectWS_Click(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( _wsEP == null ) _wsEP = lrre.WebService;

			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_logwmeth);
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.AllowedTypes = new ItemType[] { ItemType.WebService };
			frm.MultiSelect = false;
			frm.SelectedItem = _wsEP;
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem is WebService ) {
				this.Refresh();
				_wsEP = frm.SelectedItem as WebService;
				showWebServiceMethod(false, true);
			}
		}

		private void btnHAInputMapping_Click(object sender, EventArgs e)
		{
			doMapping(true, false);
			picHAInputMapOK.Image =
				( _logwmeth.InputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnHAInputView_Click(object sender, EventArgs e)
		{
			HumanActivity hact = _logwmeth.GetAssignedHumanActivity();
			showSchema(hact.FormSchema, "Human Activity Form Schema");
		}

		private void btnHAOutputMapping_Click(object sender, EventArgs e)
		{
			doMapping(false, false);
			picHAOutputMapOK.Image =
				( _logwmeth.InputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnHAOutputView_Click(object sender, EventArgs e)
		{
			HumanActivity hact = _logwmeth.GetAssignedHumanActivity();
			showSchema(hact.FormSchema, "Human Activity Form Schema");
		}

		private void btnMsmqMapping_Click(object sender, EventArgs e)
		{
			msmqMapping(true);
		}

		private void btnMsmqReceiveMapping_Click(object sender, EventArgs e)
		{
			msmqMapping(false);
		}

		private void btnProxyWMInputMapping_Click(object sender, EventArgs e)
		{
			doMapping(true, true);
			picProxyWMInputMapOK.Image =
				( _logwmeth.ProxyInputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnProxyWMInputView_Click(object sender, EventArgs e)
		{
			WebMethod wm = _logwmeth.GetAssignedProxyWebMethod();
			if ( wm != null && wm.InputSchema != null ) {
				showSchema(wm.InputSchema, "Input Contract for Proxy Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnProxyWMOutputMapping_Click(object sender, EventArgs e)
		{
			doMapping(false, true);
			picProxyWMOutputMapOK.Image =
				( _logwmeth.ProxyOutputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnProxyWMOutputView_Click(object sender, EventArgs e)
		{
			WebMethod wm = _logwmeth.GetAssignedProxyWebMethod();
			WebMethod wmCallback = _logwmeth.GetProxyCallbackWebMethod();
			if ( wm != null && wm.OutputSchema != null ) {
				showSchema(wm.OutputSchema, "Output Contract for Proxy Web Method \"" + wm.Name + "\".");
			} else if ( wmCallback != null && wmCallback.OutputSchema != null ) {
				showSchema(wmCallback.OutputSchema, "Output Contract for Proxy Callback Web Method \"" + wmCallback.Name + "\".");
			}
		}

		private void btnRemoveInput_Click(object sender, EventArgs e)
		{
			_logwmeth.InputBusinessObject = null;
			showInputOutput();
		}

		private void btnRemoveOutput_Click(object sender, EventArgs e)
		{
			_logwmeth.OutputBusinessObject = null;
			showInputOutput();
		}

		private void btnSelInput_Click(object sender, EventArgs e)
		{
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(_logwmeth);
			frm.AllowedTypes = new ItemType[] { ItemType.BObject };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = _logwmeth.InputBusinessObject;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_logwmeth.InputBusinessObject = frm.SelectedItem as BusinessObject;
				showInputOutput();
			}
		}

		private void btnSelOutput_Click(object sender, EventArgs e)
		{
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(_logwmeth);
			frm.AllowedTypes = new ItemType[] { ItemType.BObject };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = _logwmeth.OutputBusinessObject;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_logwmeth.OutputBusinessObject = frm.SelectedItem as BusinessObject;
				showInputOutput();
			}
		}

		private void btnSelectEndpointRule_Click(object sender, EventArgs e)
		{
			OpenComposite.EII.Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(_logwmeth);
			frm.AllowChangeType = false;
			frm.AllowChangeDomain = true;
			frm.AllowCreateNewItem = false;
			frm.AllowedTypes = new OpenComposite.EII.Repository.ItemType[] {
				OpenComposite.EII.Repository.ItemType.BRule, OpenComposite.EII.Repository.ItemType.LogicalWebMethod };
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.MultiSelect = false;
			frm.SelectedItem = _endpointRule;

			if ( frm.ShowDialog() == DialogResult.OK ) {
				_endpointRule = frm.SelectedItem as RepositoryBusinessRule;
				_logwmeth.InputXSLT = null;
				_logwmeth.OutputXSLT = null;
				_logwmeth.InputConnections = null;
				_logwmeth.OutputConnections = null;
				displayEndpointRule();
				getLogicalRuleResultEndpoints();
			}
		}

		private void btnSelectHumanAct_Click(object sender, EventArgs e)
		{
			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_logwmeth);
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.AllowedTypes = new ItemType[] { ItemType.HumanActivity };
			frm.MultiSelect = false;
			frm.SelectedItem = _logwmeth.GetAssignedHumanActivity();
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem is HumanActivity ) {
				this.Refresh();
				_logwmeth.HumanActivityID = frm.SelectedItem.ID;
				showHumanActivity();
			}
		}

		private void btnSelectMsmqReceive_Click(object sender, EventArgs e)
		{
			_receiveMessageQueue = selectMessageQueue(_receiveMessageQueue);
			_logwmeth.InputXSLT = null;
			_logwmeth.OutputXSLT = null;
			_logwmeth.InputConnections = null;
			_logwmeth.OutputConnections = null;

			showMessageQueue();
		}

		private void btnSelectMsmq_Click(object sender, EventArgs e)
		{
			_sendMessageQueue = selectMessageQueue(_sendMessageQueue);
			_logwmeth.InputXSLT = null;
			_logwmeth.OutputXSLT = null;
			_logwmeth.InputConnections = null;
			_logwmeth.OutputConnections = null;

			showMessageQueue();
		}

		// proxy web service
		private void btnSelectProxyWebService_Click(object sender, EventArgs e)
		{
			if ( _logwmeth.OutputXSLT != null || _logwmeth.InputXSLT != null ) {
				if ( MessageBox.Show("Changing the Proxy Web Service bound to a Logical Method will delete all mappings of the Logical Method. Do you want to continue?",
					"Change Proxy Web Service", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No ) return;
			}

			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_logwmeth);
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.AllowedTypes = new ItemType[] { ItemType.WebService };
			frm.MultiSelect = false;
			frm.SelectedItem = _wsProxy;
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem is WebService ) {
				this.Refresh();
				_wsProxy = (WebService)frm.SelectedItem;
				_logwmeth.InputXSLT = null;
				_logwmeth.OutputXSLT = null;
				_logwmeth.InputConnections = null;
				_logwmeth.OutputConnections = null;
				showInputOutput();
				showWebServiceMethod(true, false);
			}
		}

		private void btnSelectWS_Click(object sender, EventArgs e)
		{
			if ( _logwmeth.OutputXSLT != null || _logwmeth.InputXSLT != null ) {
				if ( MessageBox.Show("Changing the Web Service bound to a Logical Method will delete all mappings of the Logical Method. Do you want to continue?",
					"Change Web Service", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No ) return;
			}

			FormSelectRepositoryEx frm = new FormSelectRepositoryEx(_logwmeth);
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.AllowedTypes = new ItemType[] { ItemType.WebService };
			frm.MultiSelect = false;
			frm.SelectedItem = _logwmeth.GetAssignedWebService();
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem is WebService ) {
				this.Refresh();
				_logwmeth.WebServiceID = frm.SelectedItem.ID;
				_logwmeth.InputXSLT = null;
				_logwmeth.OutputXSLT = null;
				_logwmeth.InputConnections = null;
				_logwmeth.OutputConnections = null;
				_logwmeth.SetAssignedWebMethod(null);
				showInputOutput();
				showWebServiceMethod(false, false);
				showWebMethod(false, false);
			}
		}

		private void btnViewInput_Click(object sender, EventArgs e)
		{
			showSchema(_logwmeth.InputBusinessObject);
		}

		private void btnViewOutput_Click(object sender, EventArgs e)
		{
			showSchema(_logwmeth.OutputBusinessObject);
		}

		private void btnWMInputMapping_Click(object sender, EventArgs e)
		{
			doMapping(true, false);
			picWMInputMapOK.Image =
				( _logwmeth.InputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnWMInputView_Click(object sender, EventArgs e)
		{
			WebMethod wm = _logwmeth.GetAssignedWebMethod();
			if ( wm != null && wm.InputSchema != null ) {
				showSchema(wm.InputSchema, "Input Contract for Web Method \"" + wm.Name + "\".");
			}
		}

		private void btnWMOutputMapping_Click(object sender, EventArgs e)
		{
			doMapping(false, false);
			picWMOutputMapOK.Image =
				( _logwmeth.OutputXSLT == null ? Resources.png_cancel : Resources.png_ok );
		}

		private void btnWMOutputView_Click(object sender, EventArgs e)
		{
			WebMethod wm = _logwmeth.GetAssignedWebMethod();
			WebMethod wmCallback = _logwmeth.GetCallbackWebMethod();
			if ( wm != null && wm.OutputSchema != null ) {
				showSchema(wm.OutputSchema, "Output Contract for Web Method \"" + wm.Name + "\".");
			} else if ( wmCallback != null && wmCallback.OutputSchema != null ) {
				showSchema(wmCallback.OutputSchema, "Output Contract for Callback Web Method \"" + wmCallback.Name + "\".");
			}
		}

		private void cmbCallbackMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ( cmbCallbackMethod.SelectedIndex < 0 )
				return;

			WebMethod wm = cmbCallbackMethod.SelectedItem as WebMethod;
			_logwmeth.SetCallbackWebMethod(wm);

			showWebMethod(false, false);
		}

		private void cmbEPCallback_SelectedIndexChanged(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;

			WebMethod wm = cmbEPCallback.SelectedItem as WebMethod;
			lrre.CallbackWebMethodID = ( wm == null ? -1 : wm.ID );

			showWebMethod(false, true);
		}

		private void cmbEPProxyCallback_SelectedIndexChanged(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;

			WebMethod wm = cmbEPProxyCallback.SelectedItem as WebMethod;
			lrre.ProxyCallbackWebMethodID = ( wm == null ? -1 : wm.ID );

			showWebMethod(true, true);
		}

		private void cmbEPProxyWM_SelectedIndexChanged(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;

			WebMethod wm = cmbEPProxyWM.SelectedItem as WebMethod;
			lrre.ProxyWebMethodID = ( wm == null ? -1 : wm.ID );

			showWebMethod(true, true);

			WebMethod wmc = cmbEPProxyCallback.SelectedItem as WebMethod;
			lrre.ProxyCallbackWebMethodID = ( wmc == null ? -1 : wmc.ID );
		}

		private void cmbEPWebMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre == null ) return;

			WebMethod wm = cmbEPWebMethod.SelectedItem as WebMethod;
			lrre.WebMethodID = ( wm == null ? -1 : wm.ID );

			showWebMethod(false, true);

			WebMethod wmc = cmbEPCallback.SelectedItem as WebMethod;
			lrre.CallbackWebMethodID = ( wmc == null ? -1 : wmc.ID );
		}

		private void cmbProxyCallbackMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ( cmbProxyCallbackMethod.SelectedIndex < 0 ) {
				_logwmeth.SetProxyCallbackWebMethod(null);
			} else {
				_logwmeth.SetProxyCallbackWebMethod(cmbProxyCallbackMethod.SelectedItem as WebMethod);
			}
			showWebMethod(true, false);
		}

		private void cmbProxyWebMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool ok = true;
			if ( cmbProxyWebMethod.SelectedIndex < 0 ) {
				ok = _logwmeth.SetAssignedProxyWebMethod(null);
			} else {
				ok = _logwmeth.SetAssignedProxyWebMethod(cmbProxyWebMethod.SelectedItem as WebMethod);
			}
			if ( !ok ) {
				cmbProxyWebMethod.SelectedItem = _logwmeth.GetAssignedProxyWebMethod();
				return;
			}
			showWebMethod(true, false);
		}

		private void cmbSelectEPRuleResult_DrawItem(object sender, DrawItemEventArgs e)
		{
			e.DrawBackground();

			e.DrawFocusRectangle();
		}

		private void cmbSelectEPRuleResult_MeasureItem(object sender, MeasureItemEventArgs e)
		{
			if ( e.Index > -1 ) {
				e.ItemHeight = 30;
			}
			e.ItemWidth = cmbSelectEPRuleResult.Width;
		}

		private void cmbSelectEPRuleResult_SelectedIndexChanged(object sender, EventArgs e)
		{
			displayCurrentLogicalRuleResultEndpoint();
		}

		private void cmbWebMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ( cmbWebMethod.SelectedIndex < 0 )
				return;

			WebMethod wm = cmbWebMethod.SelectedItem as WebMethod;
			if ( !_logwmeth.SetAssignedWebMethod(wm) ) {
				cmbWebMethod.SelectedItem = _logwmeth.GetAssignedWebMethod();
				return;
			}

			showWebMethod(false, false);

			WebMethod wmc = cmbCallbackMethod.SelectedItem as WebMethod;
			_logwmeth.SetCallbackWebMethod(wmc);

			showInputOutput();
		}

		private void tabAssignment_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void tabControl1_Selected(object sender, TabControlEventArgs e)
		{
			if ( e.TabPage == tabUsedBy ) {
				usedByHierarchyLogicalMethod.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( e.TabPage == tabUses ) {
				usingHierarchyLogicalMethod.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}
		}

		private void txtBO_DragDrop(object sender, DragEventArgs e)
		{
			if ( e.Data.GetDataPresent(typeof(BusinessObject)) ) {
				BusinessObject bo = (BusinessObject)e.Data.GetData(typeof(BusinessObject));
				if ( sender == txtInput ) {
					_logwmeth.InputBusinessObject = bo;
				} else if ( sender == txtOutput ) {
					_logwmeth.OutputBusinessObject = bo;
				}
				showInputOutput();
			}
		}

		private void txtBO_DragOver(object sender, DragEventArgs e)
		{
			if ( e.Data.GetDataPresent(typeof(BusinessObject)) ) {
				e.Effect = DragDropEffects.Link;
			} else {
				e.Effect = DragDropEffects.None;
			}
		}

		private void txtVerbName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#endregion Event Handlers

		#region Methods

		private XmlSchemaSet createXmlSchemaSet()
		{
			XsdDataContractExporter ex = new XsdDataContractExporter();
			ex.Export(typeof(OpenComposite.EII.Designer.Workflow.Help));
			XmlSchemaSet xsdset = ex.Schemas;
			XmlSchema xsdRemove = null;
			foreach ( XmlSchema xsd in xsdset.Schemas() ) {
				if ( xsd.Elements.Contains(new XmlQualifiedName("Help", xsd.TargetNamespace)) ) {
					xsdRemove = xsd;
				}
			}
			xsdset.Remove(xsdRemove);
			return xsdset;
		}

		private void displayCurrentLogicalRuleResultEndpoint()
		{
			LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
			if ( lrre != null ) {
				_wsEP = lrre.WebService;
				_wsEPProxy = lrre.ProxyWebService;
			}
			showWebServiceMethod(false, true);
			showWebServiceMethod(true, true);
		}

		private void displayEndpointRule()
		{
			if ( _endpointRule == null ) {
				txtEndpointRule.TextAlign = HorizontalAlignment.Center;
				txtEndpointRule.ForeColor = SystemColors.GrayText;
				txtEndpointRule.Text = "Please select a selection rule";
				cmbSelectEPRuleResult.Items.Clear();
				grpEPRuleResultAssignment.Enabled = false;
			} else {
				txtEndpointRule.TextAlign = HorizontalAlignment.Left;
				txtEndpointRule.ForeColor = SystemColors.WindowText;
				txtEndpointRule.Text = _endpointRule.Name;
				cmbSelectEPRuleResult.BeginUpdate();
				cmbSelectEPRuleResult.Items.Clear();
				cmbSelectEPRuleResult.Items.AddRange(_endpointRule.Results.Split(';'));
				cmbSelectEPRuleResult.EndUpdate();
				grpEPRuleResultAssignment.Enabled = true;
				if ( cmbSelectEPRuleResult.Items.Count > 0 ) {
					cmbSelectEPRuleResult.SelectedIndex = 0;
				}
			}
		}

		private void doMapping(bool isInput, bool isProxy)
		{
			WebService webService = null;
			WebMethod wmeth = null;
			WebMethod wmCallback = null;
			HumanActivity hact = null;
			XmlSchema xsdInput = null, xsdOutput = null;
			string cancelmsg;
			if ( tabCtrlAssignment.SelectedTab == tabAssignHA ) {
				hact = _logwmeth.GetAssignedHumanActivity();
				cancelmsg = null;
				if ( hact == null ) {
					cancelmsg = "Please select an human activity.";
				} else if ( hact.FormSchema == null ) {
					cancelmsg = "The selected human activity form doesn't contains a form schema definition.";
				} else if ( isInput && _logwmeth.InputBusinessObject == null ) {
					cancelmsg = "This logical method has no input business object.";
				} else if ( !isInput && _logwmeth.OutputBusinessObject == null ) {
					cancelmsg = "This logical method has no output business object.";
				}
				if ( cancelmsg != null ) {
					MessageBox.Show(cancelmsg, "Logical Method Mapping", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}
				xsdInput = xsdOutput = hact.FormSchema;
			} else {
				if ( tabCtrlAssignment.SelectedTab == tabAssignEPRule ) {
					LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
					if ( lrre != null ) {
						if ( isProxy ) {
							wmeth = lrre.ProxyWebMethod;
							wmCallback = lrre.ProxyCallbackWebMethod;
							webService = _wsEPProxy;
						} else {
							wmeth = lrre.WebMethod;
							wmCallback = lrre.CallbackWebMethod;
							webService = _wsEP;
						}
					}
				} else {
					if ( isProxy ) {
						wmeth = _logwmeth.GetAssignedProxyWebMethod();
						wmCallback = _logwmeth.GetProxyCallbackWebMethod();
						webService = _wsProxy;
					} else {
						wmeth = _logwmeth.GetAssignedWebMethod();
						wmCallback = _logwmeth.GetCallbackWebMethod();
						webService = _logwmeth.GetAssignedWebService();
					}
				}
				if ( webService == null ) {
					webService = wmeth.WebService;
				}
				if ( ( wmeth == null ||
					 ( isInput && ( wmeth.InputSchema == null || _logwmeth.InputBusinessObject == null ) ) ||
					 ( !isInput && ( wmeth.OutputSchema == null || _logwmeth.OutputBusinessObject == null ) ) ) &&
					 ( wmCallback == null ||
					 ( !isInput && ( wmCallback.OutputSchema == null || _logwmeth.OutputBusinessObject == null ) ) ) )
					return;
				xsdInput = wmeth.InputSchema;
				if ( wmCallback != null ) {
					xsdOutput = wmCallback.OutputSchema;
				} else {
					xsdOutput = wmeth.OutputSchema;
				}
			}

			string connections = null, sXslt = null;
			XmlSchema xsdSource = null, xsdTarget = null;
			//XmlSchema xsdIn, xsdOut;
			FormMapping frm = new FormMapping();
			if ( tabCtrlAssignment.SelectedTab == tabAssignEPRule ) {
				LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
				if ( lrre != null ) {
					if ( isInput ) {
						if ( _logwmeth.InputBusinessObjectID <= 0 || xsdInput == null )
							return;
						if ( isProxy ) {
							sXslt = lrre.ProxyInputXSLT == null ? null : lrre.ProxyInputXSLT.InnerXml;
							connections = lrre.ProxyInputConnections;
						} else {
							sXslt = lrre.InputXSLT == null ? null : lrre.InputXSLT.InnerXml;
							connections = lrre.InputConnections;
						}
						xsdSource = _logwmeth.InputBusinessObject.Schema;
						xsdTarget = xsdInput;
					} else {
						if ( _logwmeth.OutputBusinessObjectID <= 0 || xsdOutput == null )
							return;
						if ( isProxy ) {
							sXslt = lrre.ProxyOutputXSLT == null ? null : lrre.ProxyOutputXSLT.OuterXml;
							connections = lrre.ProxyOutputConnections;
						} else {
							sXslt = lrre.OutputXSLT == null ? null : lrre.OutputXSLT.OuterXml;
							connections = lrre.OutputConnections;
						}
						xsdSource = xsdOutput;
						xsdTarget = _logwmeth.OutputBusinessObject.Schema;
					}
				}
			} else {
				if ( isInput ) {
					if ( _logwmeth.InputBusinessObjectID <= 0 || xsdInput == null )
						return;
					if ( isProxy ) {
						sXslt = _logwmeth.ProxyInputXSLT == null ? null : _logwmeth.ProxyInputXSLT.InnerXml;
						connections = _logwmeth.ProxyInputConnections;
					} else {
						sXslt = _logwmeth.InputXSLT == null ? null : _logwmeth.InputXSLT.InnerXml;
						connections = _logwmeth.InputConnections;
					}
					xsdSource = _logwmeth.InputBusinessObject.Schema;
					xsdTarget = xsdInput;
				} else {
					if ( _logwmeth.OutputBusinessObjectID <= 0 || xsdOutput == null )
						return;
					if ( isProxy ) {
						sXslt = _logwmeth.ProxyOutputXSLT == null ? null : _logwmeth.ProxyOutputXSLT.OuterXml;
						connections = _logwmeth.ProxyOutputConnections;
					} else {
						sXslt = _logwmeth.OutputXSLT == null ? null : _logwmeth.OutputXSLT.OuterXml;
						connections = _logwmeth.OutputConnections;
					}
					xsdSource = xsdOutput;
					xsdTarget = _logwmeth.OutputBusinessObject.Schema;
				}
			}
			MetadataSet mset = null;
			System.ServiceModel.Description.WsdlImporter importer = null;
			XmlSchemaSet xsdSet = null;
			if ( webService != null ) {
				if ( webService.Type == WebService.ServiceType.WebService ) {
					System.Web.Services.Description.ServiceDescription wsdl = null;
					using ( StringReader sr = new StringReader(webService.MetaData) )
					using ( XmlReader xr = XmlReader.Create(sr) ) {
						mset = MetadataSet.ReadFrom(xr);
					}
					if ( wsdl == null ) {
						importer = new System.ServiceModel.Description.WsdlImporter(mset);
						if ( importer.WsdlDocuments.Count > 0 ) {
							wsdl = importer.WsdlDocuments[0];
						}
					}
					// found wsdl document?
					if ( wsdl != null ) {
						xsdSet = createXmlSchemaSet();

						//add WSDL Document Namespaces to the Schema Builder
						foreach ( XmlSchema xsd in wsdl.Types.Schemas ) {
							if ( !xsdSet.Contains(xsd.TargetNamespace) )
								xsdSet.Add(xsd);
						}
						if ( importer != null ) {
							foreach ( XmlSchema xsd in importer.XmlSchemas.Schemas() ) {
								if ( !xsdSet.Contains(xsd.TargetNamespace) )
									xsdSet.Add(xsd);
							}
						}
						xsdSet.Compile();
					}
				}
			}
			frm.InitializeMapper(xsdSource, ( isInput ? null : xsdSet ),
								 xsdTarget, ( isInput ? xsdSet : null ),
								 sXslt, connections);
			//frm.SetMappingText( _logwmeth, isInput);
			ISupportLogicalPhysicalMapping mappingItem = tabCtrlAssignment.SelectedTab == tabAssignEPRule ? (ISupportLogicalPhysicalMapping)getCurrentSelectedResultEP() : (ISupportLogicalPhysicalMapping)_logwmeth;
			frm.SetMappingText(mappingItem, isInput);
			//frm.HeaderText = _logwmeth.GenerateHeaderText(isInput);
			frm.UseCodeMapping = isInput ? mappingItem.UseInputCodeMapping : mappingItem.UseOutputCodeMapping;
			//frm.InitializeMapper(xsdSource, xsdTarget, connections);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				this.TopLevelControl.Refresh();
				XsltDocument xslt = frm.GetMapping(out connections);
				if ( tabCtrlAssignment.SelectedTab == tabAssignEPRule ) {
					LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
					if ( lrre != null ) {
						if ( isInput ) {
							if ( isProxy ) {
								lrre.ProxyInputConnections = connections;
								lrre.ProxyInputXSLT = xslt;
							} else {
								lrre.InputConnections = connections;
								lrre.InputXSLT = xslt;
							}
							lrre.UseInputCodeMapping = frm.UseCodeMapping;
							if ( frm.UseCodeMapping ) {
								lrre.InputCodeMapping = frm.GetCodeMapping();
							} else {
								lrre.InputCodeMapping = null;
							}
						} else {
							if ( isProxy ) {
								lrre.ProxyOutputConnections = connections;
								lrre.ProxyOutputXSLT = xslt;
							} else {
								lrre.OutputConnections = connections;
								lrre.OutputXSLT = xslt;
							}
							lrre.UseOutputCodeMapping = frm.UseCodeMapping;
							if ( frm.UseCodeMapping ) {
								lrre.OutputCodeMapping = frm.GetCodeMapping();
							} else {
								lrre.OutputCodeMapping = null;
							}
						}
					}
				} else {
					if ( isInput ) {
						if ( isProxy ) {
							_logwmeth.ProxyInputConnections = connections;
							_logwmeth.ProxyInputXSLT = xslt;
						} else {
							_logwmeth.InputConnections = connections;
							_logwmeth.InputXSLT = xslt;
						}
						_logwmeth.ExtendedProperties.UseInputCodeMapping = frm.UseCodeMapping;
						if ( frm.UseCodeMapping ) {
							_logwmeth.ExtendedProperties.InputCodeMapping = frm.GetCodeMapping();
						} else {
							_logwmeth.ExtendedProperties.InputCodeMapping = null;
						}
					} else {
						if ( isProxy ) {
							_logwmeth.ProxyOutputConnections = connections;
							_logwmeth.ProxyOutputXSLT = xslt;
						} else {
							_logwmeth.OutputConnections = connections;
							_logwmeth.OutputXSLT = xslt;
						}
						_logwmeth.ExtendedProperties.UseOutputCodeMapping = frm.UseCodeMapping;
						if ( frm.UseCodeMapping ) {
							_logwmeth.ExtendedProperties.OutputCodeMapping = frm.GetCodeMapping();
						} else {
							_logwmeth.ExtendedProperties.OutputCodeMapping = null;
						}
					}
				}
				_logwmeth.Save();
			}
		}

		private void enableHAInputOutput(bool enabled)
		{
			if ( !enabled ) txtHAInput.Text = "";
			txtHAInput.Enabled = enabled;
			btnHAInputView.Enabled = enabled;
			btnHAInputMapping.Enabled = enabled;
			if ( !enabled ) picHAInputMapOK.Image = Resources.png_cancel;
			picHAInputMapOK.Enabled = enabled;

			if ( !enabled ) txtHAOutput.Text = "";
			txtHAOutput.Enabled = enabled;
			btnHAOutputView.Enabled = enabled;
			btnHAOutputMapping.Enabled = enabled;
			if ( !enabled ) picHAOutputMapOK.Image = Resources.png_cancel;
			picHAOutputMapOK.Enabled = enabled;
		}

		private void enableMsmqMapping(bool send, bool enabled)
		{
			if ( send ) {
				btnMsmqSendMapping.Enabled = enabled;
			} else {
				btnMsmqReceiveMapping.Enabled = enabled;
			}
		}

		private void enableWMInputOutput(bool enabled, bool isInput, bool isProxy, bool isEPRule)
		{
			TextBox txt;
			Button btnView, btnMapping;
			PictureBox picMapOK;

			#region get, btnView, btnMapping, picMapOK
			if ( isEPRule ) {
				if ( isProxy ) {
					txt = isInput ? txtEPProxyIn : txtEPProxyOut;
					btnView = isInput ? btnEPProxyInView : btnEPProxyOutView;
					btnMapping = isInput ? btnEPProxyInMap : btnEPProxyOutMap;
					picMapOK = isInput ? picEPProxyInIsMapped : picEPProxyOutIsMapped;
				} else {
					txt = isInput ? txtEPInput : txtEPOutput;
					btnView = isInput ? btnEPInputView : btnEPOutputView;
					btnMapping = isInput ? btnEPInputMap : btnEPOutputMap;
					picMapOK = isInput ? picEPInputIsMapped : picEPOutputIsMapped;
				}
			} else {
				if ( isProxy ) {
					txt = isInput ? txtProxyWMInput : txtProxyWMOutput;
					btnView = isInput ? btnProxyWMInputView : btnProxyWMOutputView;
					btnMapping = isInput ? btnProxyWMInputMapping : btnProxyWMOutputMapping;
					picMapOK = isInput ? picProxyWMInputMapOK : picProxyWMOutputMapOK;
				} else {
					txt = isInput ? txtWMInput : txtWMOutput;
					btnView = isInput ? btnWMInputView : btnWMOutputView;
					btnMapping = isInput ? btnWMInputMapping : btnWMOutputMapping;
					picMapOK = isInput ? picWMInputMapOK : picWMOutputMapOK;
				}
			}
			#endregion

			if ( !enabled ) txt.Text = "";
			txt.Enabled = enabled;
			btnView.Enabled = enabled;
			btnMapping.Enabled = enabled;
			if ( !enabled ) picMapOK.Image = Resources.png_cancel;
			picMapOK.Enabled = enabled;
		}

		private void enableWebMethod(bool enabled, bool isProxy, bool isEPRule)
		{
			if ( isEPRule ) {
				if ( isProxy ) {
					if ( !enabled ) cmbEPProxyWM.Items.Clear();
					cmbEPProxyWM.Enabled = enabled;
					if ( !enabled ) cmbEPProxyCallback.Items.Clear();
					cmbEPProxyCallback.Enabled = enabled;
				} else {
					if ( !enabled ) cmbEPWebMethod.Items.Clear();
					cmbEPWebMethod.Enabled = enabled;
					if ( !enabled ) cmbEPCallback.Items.Clear();
					cmbEPCallback.Enabled = enabled;
				}
			} else {
				if ( isProxy ) {
					if ( !enabled ) cmbProxyWebMethod.Items.Clear();
					cmbProxyWebMethod.Enabled = enabled;
					if ( !enabled ) cmbProxyCallbackMethod.Items.Clear();
					cmbProxyCallbackMethod.Enabled = enabled;
				} else {
					if ( !enabled ) cmbWebMethod.Items.Clear();
					cmbWebMethod.Enabled = enabled;
					if ( !enabled ) cmbCallbackMethod.Items.Clear();
					cmbCallbackMethod.Enabled = enabled;
				}
			}
		}

		private LogicalRuleResultEndpoint getCurrentSelectedResultEP()
		{
			string selectedResult = cmbSelectEPRuleResult.SelectedItem as string;
			if ( selectedResult == null || !_ruleResultEndpoints.ContainsKey(selectedResult) ) {
				return null;
			}
			return _ruleResultEndpoints[selectedResult];
		}

		private void getLogicalRuleResultEndpoints()
		{
			if ( _endpointRule == null ) return;
			if ( _ruleResultEndpoints == null ) {
				_ruleResultEndpoints = new Dictionary<string, LogicalRuleResultEndpoint>();
			} else {
				_ruleResultEndpoints.Clear();
			}
			BindingListEx<LogicalRuleResultEndpoint> items = _logwmeth.GetLogicalRuleResultEndpoints();
			List<string> results = new List<string>(_endpointRule.Results.Split(';'));
			if ( items == null || items.Count == 0 ) {
				foreach ( string result in results ) {
					_ruleResultEndpoints.Add(
						result, new LogicalRuleResultEndpoint(_logwmeth, _logwmeth.ID, result));
				}
			} else {
				foreach ( var item in items.ToArray() ) {
					if ( results.Contains(item.EndpointRuleResult) &&
						 !_ruleResultEndpoints.ContainsKey(item.EndpointRuleResult) ) {
						_ruleResultEndpoints.Add(item.EndpointRuleResult, item);
					} else if ( !results.Contains(item.EndpointRuleResult) ) {
						items.Remove(item);
					}
				}
				if ( results.Count > _ruleResultEndpoints.Count ) {
					foreach ( string result in results ) {
						if ( !_ruleResultEndpoints.ContainsKey(result) ) {
							_ruleResultEndpoints.Add(
								result, new LogicalRuleResultEndpoint(_logwmeth, _logwmeth.ID, result));
						}
					}
				}
			}
		}

		private void init()
		{
			_ruleResultEndpoints = new Dictionary<string, LogicalRuleResultEndpoint>();
		}

		private void load()
		{
			this.SuspendLayout();
			try {
				if ( _logwmeth == null )
					return;

				_ruleResultEndpoints = new Dictionary<string, LogicalRuleResultEndpoint>();
				_wsProxy = _logwmeth.GetAssignedProxyWebService();
				_endpointRule = _logwmeth.GetEndpointRule();
				_sendMessageQueue = _logwmeth.SendMessageQueue;
				_receiveMessageQueue = _logwmeth.ReceiveMessageQueue;
				displayEndpointRule();

				txtVerbName.Text = _logwmeth.Name;
				txtVerbDesc.Text = _logwmeth.Description;
				showInputOutput();
				showHumanActivity();
				showMessageQueue();
				showWebServiceMethod(false, false);
				showWebServiceMethod(true, false);
				displayEndpointRule();
				showWebServiceMethod(false, true);
				showWebServiceMethod(true, true);
				showWebMethod(false, false);
				showWebMethod(true, false);
				showWebMethod(false, true);
				showWebMethod(true, true);
				if ( _logwmeth.IsAssignedTo == LogicalMethodAssignedTo.EndpointRule ) {
					tabCtrlAssignment.SelectedTab = tabAssignEPRule;
					getLogicalRuleResultEndpoints();
					displayCurrentLogicalRuleResultEndpoint();
				} else if ( _logwmeth.IsAssignedTo == LogicalMethodAssignedTo.HumanActivity ) {
					tabCtrlAssignment.SelectedTab = tabAssignHA;
				} else if ( _logwmeth.IsAssignedTo == LogicalMethodAssignedTo.MessageQueue ) {
					tabCtrlAssignment.SelectedTab = tabAssignMessageQueue;
				} else {
					tabCtrlAssignment.SelectedTab = tabAssignWS;
				}
			} finally {
				this.ResumeLayout(true);
			}
			if ( tabControl1.SelectedTab == tabUsedBy ) {
				usedByHierarchyLogicalMethod.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( tabControl1.SelectedTab == tabUses ) {
				usingHierarchyLogicalMethod.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}
		}

		private void msmqMapping(bool isInput)
		{
			XmlSchema xsdInput = null, xsdOutput = null;
			XmlSchema xsdSource = null, xsdTarget = null;
			string sXslt = null;
			string connections = null;
			FormMapping frm = new FormMapping();
			if ( isInput ) {
				if ( _logwmeth.InputBusinessObject != null ) {
					xsdInput = _logwmeth.InputBusinessObject.Schema;
					if ( _logwmeth.InputBusinessObjectID <= 0 || xsdInput == null )
						return;
					sXslt = _logwmeth.InputXSLT == null ? null : _logwmeth.InputXSLT.InnerXml;
					xsdSource = _logwmeth.InputBusinessObject.Schema;
					xsdTarget = xsdInput;
				}
				connections = _logwmeth.InputConnections;
			} else {
				if ( _logwmeth.OutputBusinessObject != null ) {
					xsdOutput = _logwmeth.OutputBusinessObject.Schema;
					if ( _logwmeth.OutputBusinessObjectID <= 0 || xsdOutput == null )
						return;
					sXslt = _logwmeth.OutputXSLT == null ? null : _logwmeth.OutputXSLT.OuterXml;
					xsdSource = xsdOutput;
					xsdTarget = _logwmeth.OutputBusinessObject.Schema;
				}
				connections = _logwmeth.OutputConnections;
			}
			XmlSchemaSet xsdSet = null;
			xsdSet = createXmlSchemaSet();

			frm.InitializeMapper(xsdSource, ( isInput ? null : xsdSet ),
								 xsdTarget, ( isInput ? xsdSet : null ),
								 sXslt, connections);
			//frm.SetMappingText( _logwmeth, isInput);
			ISupportLogicalPhysicalMapping mappingItem = tabCtrlAssignment.SelectedTab == tabAssignEPRule ? (ISupportLogicalPhysicalMapping)getCurrentSelectedResultEP() : (ISupportLogicalPhysicalMapping)_logwmeth;
			frm.SetMappingText(mappingItem, isInput);
			//frm.HeaderText = _logwmeth.GenerateHeaderText(isInput);
			frm.UseCodeMapping = true;
			//frm.InitializeMapper(xsdSource, xsdTarget, connections);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				this.TopLevelControl.Refresh();
				XsltDocument xslt = frm.GetMapping(out connections);
				if ( isInput ) {
					_logwmeth.InputConnections = connections;
					_logwmeth.InputXSLT = xslt;
					_logwmeth.ExtendedProperties.UseInputCodeMapping = frm.UseCodeMapping;
					if ( frm.UseCodeMapping ) {
						_logwmeth.ExtendedProperties.InputCodeMapping = frm.GetCodeMapping();
					} else {
						_logwmeth.ExtendedProperties.InputCodeMapping = null;
					}
				} else {
					_logwmeth.OutputConnections = connections;
					_logwmeth.OutputXSLT = xslt;
					_logwmeth.ExtendedProperties.UseOutputCodeMapping = frm.UseCodeMapping;
					if ( frm.UseCodeMapping ) {
						_logwmeth.ExtendedProperties.OutputCodeMapping = frm.GetCodeMapping();
					} else {
						_logwmeth.ExtendedProperties.OutputCodeMapping = null;
					}
				}
				_logwmeth.Save();
			}
			showMessageQueue();
		}

		//private void enableWMOutput(bool enabled, bool isProxy, bool isEPRule)
		//{
		//	if ( isProxy ) {
		//		if ( !enabled ) txtProxyWMOutput.Text = "";
		//		txtProxyWMOutput.Enabled = enabled;
		//		btnProxyWMOutputView.Enabled = enabled;
		//		btnProxyWMOutputMapping.Enabled = enabled;
		//		if ( !enabled ) picProxyWMOutputMapOK.Image = Resources.png_cancel;
		//		picProxyWMOutputMapOK.Enabled = enabled;
		//	} else {
		//		if ( !enabled ) txtWMOutput.Text = "";
		//		txtWMOutput.Enabled = enabled;
		//		btnWMOutputView.Enabled = enabled;
		//		btnWMOutputMapping.Enabled = enabled;
		//		if ( !enabled ) picWMOutputMapOK.Image = Resources.png_cancel;
		//		picWMOutputMapOK.Enabled = enabled;
		//	}
		//}
		private void save()
		{
			Debug.Assert(_logwmeth != null);

			int inid = _logwmeth.InputBusinessObjectID;
			int outid = _logwmeth.OutputBusinessObjectID;
			int wsvcid = _logwmeth.WebServiceID;
			int wmethid = _logwmeth.WebMethodID;
			int wmcallid = _logwmeth.CallbackWebMethodID;
			int hactid = _logwmeth.HumanActivityID;
			int proxywmethid = _logwmeth.ProxyWebMethodID;
			int proxywmcallid = _logwmeth.ProxyCallbackWebMethodID;
			int sendMsmqid = _logwmeth.SendMessageQueueID;
			int receiveMsmqid = _logwmeth.ReceiveMessageQueueID;
			if ( wsvcid <= 0 && wmethid > 0 ) {
				wsvcid = _logwmeth.GetAssignedWebMethod().WebServiceID;
			}
			_logwmeth.Reload();

			_logwmeth.Name = txtVerbName.Text;
			_logwmeth.Description = txtVerbDesc.Text;
			_logwmeth.InputBusinessObjectID = inid;
			_logwmeth.OutputBusinessObjectID = outid;

			if ( tabCtrlAssignment.SelectedTab == tabAssignEPRule ) {
				_logwmeth.WebServiceID = -1;
				_logwmeth.WebMethodID = -1;
				_logwmeth.CallbackWebMethodID = -1;
				_logwmeth.ProxyWebMethodID = -1;
				_logwmeth.ProxyCallbackWebMethodID = -1;
				_logwmeth.HumanActivityID = -1;
				_logwmeth.SendMessageQueueID = -1;
				_logwmeth.ReceiveMessageQueueID = -1;
				_logwmeth.EndpointRuleID = ( _endpointRule != null ? _endpointRule.ID : -1 );
				_logwmeth.SetLogicalRuleResultEndpoint(
					new List<LogicalRuleResultEndpoint>(_ruleResultEndpoints.Values));
			} else if ( tabCtrlAssignment.SelectedTab == tabAssignHA ) {
				_logwmeth.WebServiceID = -1;
				_logwmeth.WebMethodID = -1;
				_logwmeth.CallbackWebMethodID = -1;
				_logwmeth.ProxyWebMethodID = -1;
				_logwmeth.ProxyCallbackWebMethodID = -1;
				_logwmeth.HumanActivityID = hactid;
				_logwmeth.EndpointRuleID = -1;
				_logwmeth.SendMessageQueueID = -1;
				_logwmeth.ReceiveMessageQueueID = -1;
				_logwmeth.SetLogicalRuleResultEndpoint(null);
			} else if ( tabCtrlAssignment.SelectedTab == tabAssignMessageQueue ) {
				_logwmeth.WebServiceID = -1;
				_logwmeth.WebMethodID = -1;
				_logwmeth.CallbackWebMethodID = -1;
				_logwmeth.ProxyWebMethodID = -1;
				_logwmeth.ProxyCallbackWebMethodID = -1;
				_logwmeth.HumanActivityID = -1;
				_logwmeth.SendMessageQueueID = -1;
				_logwmeth.ReceiveMessageQueueID = -1;
				_logwmeth.EndpointRuleID = -1;
				_logwmeth.SetLogicalRuleResultEndpoint(null);
				_logwmeth.SendMessageQueueID = _sendMessageQueue == null ? -1 : _sendMessageQueue.ID;
				_logwmeth.ReceiveMessageQueueID = _receiveMessageQueue == null ? -1 : _receiveMessageQueue.ID;
			} else {
				_logwmeth.WebServiceID = wsvcid;
				_logwmeth.WebMethodID = wmethid;
				_logwmeth.CallbackWebMethodID = wmcallid;
				_logwmeth.ProxyWebMethodID = proxywmethid;
				_logwmeth.ProxyCallbackWebMethodID = proxywmcallid;
				_logwmeth.HumanActivityID = -1;
				_logwmeth.EndpointRuleID = -1;
				_logwmeth.SendMessageQueueID = -1;
				_logwmeth.ReceiveMessageQueueID = -1;
				_logwmeth.SetLogicalRuleResultEndpoint(null);
			}
		}

		private void saveCurrentLogicalRuleResultEndpoint()
		{
		}

		private MessageQueueEx selectMessageQueue(MessageQueueEx selected)
		{
			OpenComposite.EII.Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(_logwmeth);
			frm.AllowChangeType = false;
			frm.AllowChangeDomain = true;
			frm.AllowCreateNewItem = false;
			frm.AllowedTypes = new ItemType[] { ItemType.MessageQueue };
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.MultiSelect = false;
			frm.SelectedItem = selected;

			MessageQueueEx value = null;
			if ( frm.ShowDialog() == DialogResult.OK && frm.SelectedItem != null ) {
				value = frm.SelectedItem as MessageQueueEx;
			}
			return value;
		}

		private void showHumanActivity()
		{
			HumanActivity hact = _logwmeth.GetAssignedHumanActivity();
			if ( hact == null ) {
				txtHumanActivity.Text = "Please select a human activity";
				txtHumanActivity.TextAlign = HorizontalAlignment.Center;
				enableHAInputOutput(false);
			} else {
				txtHumanActivity.Text = hact.Name;
				txtHumanActivity.TextAlign = HorizontalAlignment.Left;
				enableHAInputOutput(!string.IsNullOrEmpty(hact.FormSchemaName));
				txtHAInput.Text = hact.FormSchemaName;
				txtHAOutput.Text = hact.FormSchemaName;
				picHAInputMapOK.Image =
					( _logwmeth.InputXSLT == null ? Resources.png_cancel : Resources.png_ok );
				picHAOutputMapOK.Image =
					( _logwmeth.OutputXSLT == null ? Resources.png_cancel : Resources.png_ok );
			}
		}

		private void showInputOutput()
		{
			this.SuspendLayout();
			txtInput.SuspendLayout();
			txtOutput.SuspendLayout();
			if ( _logwmeth.InputBusinessObject == null ) {
				txtInput.ForeColor = SystemColors.GrayText;
				txtInput.TextAlign = HorizontalAlignment.Center;
				txtInput.Text = "Please select an input.";
				btnRemoveInput.Enabled = false;
				btnViewInput.Enabled = false;
			} else {
				txtInput.ForeColor = SystemColors.ControlText;
				txtInput.TextAlign = HorizontalAlignment.Left;
				txtInput.Text = _logwmeth.InputBusinessObject.Name;
				btnRemoveInput.Enabled = true;
				btnViewInput.Enabled = true;
			}
			if ( _logwmeth.OutputBusinessObject == null ) {
				txtOutput.ForeColor = SystemColors.GrayText;
				txtOutput.TextAlign = HorizontalAlignment.Center;
				txtOutput.Text = "Please select an output.";
				btnRemoveOutput.Enabled = false;
				btnViewOutput.Enabled = false;
			} else {
				txtOutput.ForeColor = SystemColors.ControlText;
				txtOutput.TextAlign = HorizontalAlignment.Left;
				txtOutput.Text = _logwmeth.OutputBusinessObject.Name;
				btnRemoveOutput.Enabled = true;
				btnViewOutput.Enabled = true;
			}
			if ( _logwmeth.InputXSLT == null ) {
				picEPHAInMapOK.Image = Resources.png_cancel;
				picEPInputIsMapped.Image = Resources.png_cancel;
				picEPProxyInIsMapped.Image = Resources.png_cancel;
				picHAInputMapOK.Image = Resources.png_cancel;
				picProxyWMInputMapOK.Image = Resources.png_cancel;
				picWMInputMapOK.Image = Resources.png_cancel;
			}
			if ( _logwmeth.OutputXSLT == null ) {
				picEPHAOutMapOK.Image = Resources.png_cancel;
				picEPOutputIsMapped.Image = Resources.png_cancel;
				picEPProxyOutIsMapped.Image = Resources.png_cancel;
				picHAOutputMapOK.Image = Resources.png_cancel;
				picProxyWMOutputMapOK.Image = Resources.png_cancel;
				picWMOutputMapOK.Image = Resources.png_cancel;
			}
			txtInput.ResumeLayout(false);
			txtOutput.ResumeLayout(false);
			this.ResumeLayout(true);
		}

		private void showMessageQueue()
		{
			if ( _sendMessageQueue == null ) {
				txtMsmqSendName.Text = "Please select a message queue";
				txtMsmqSendName.TextAlign = HorizontalAlignment.Center;
				picMsmqSendIsMapped.Image = Resources.png_cancel;
				enableMsmqMapping(true, false);
			} else {
				txtMsmqSendName.Text = _sendMessageQueue.Name;
				txtMsmqSendName.TextAlign = HorizontalAlignment.Left;
				if ( !string.IsNullOrEmpty(_logwmeth.ExtendedProperties.InputCodeMapping) ) {
					picMsmqSendIsMapped.Image = Resources.png_ok;
				}
				enableMsmqMapping(true, true);
			}
			if ( _receiveMessageQueue == null ) {
				txtMsmqReceiveName.Text = "Please select a message queue";
				txtMsmqReceiveName.TextAlign = HorizontalAlignment.Center;
				picMsmqReceiveIsMapped.Image = Resources.png_cancel;
				enableMsmqMapping(false, false);
			} else {
				txtMsmqReceiveName.Text = _receiveMessageQueue.Name;
				txtMsmqReceiveName.TextAlign = HorizontalAlignment.Left;
				if ( !string.IsNullOrEmpty(_logwmeth.ExtendedProperties.OutputCodeMapping) ) {
					picMsmqReceiveIsMapped.Image = Resources.png_ok;
				}
				enableMsmqMapping(false, true);
			}
		}

		private void showSchema(BusinessObject bo)
		{
			if ( bo == null || bo.Schema == null )
				return;
			showSchema(bo.Schema, "XML-Schema of Business Object \"" + bo.Name + "\"");
		}

		private void showSchema(XmlSchema schema, string formText)
		{
			XmlDocument xd = new XmlDocument();
			using ( StringWriter sw = new StringWriter() ) {
				schema.Write(sw);
				xd.LoadXml(sw.ToString());
			}
			formXmlViewer XmlViewer = new formXmlViewer();
			XmlViewer.XmlDocument = xd;
			XmlViewer.Text = formText;
			XmlViewer.ShowDialog();
		}

		private void showWebMethod(bool isProxy, bool isEPRule)
		{
			// get assigned web method
			LogicalRuleResultEndpoint lrre = null;
			WebMethod wm = null;
			WebMethod wmCallback = null;
			#region get webmethod and callback webmethod
			if ( isEPRule ) {
				lrre = getCurrentSelectedResultEP();
				if ( lrre != null ) {
					if ( isProxy ) {
						wm = lrre.ProxyWebMethod;
						wmCallback = lrre.ProxyCallbackWebMethod;
					} else {
						wm = lrre.WebMethod;
						wmCallback = lrre.CallbackWebMethod;
					}
				}
			} else {
				if ( isProxy ) {
					wm = _logwmeth.GetAssignedProxyWebMethod();
					wmCallback = _logwmeth.GetProxyCallbackWebMethod();
				} else {
					wm = _logwmeth.GetAssignedWebMethod();
					wmCallback = _logwmeth.GetCallbackWebMethod();
				}
			}
			#endregion
			ComboBox cmbwm, cmbcallbackwm;
			Label lblcallback;
			#region set cmbwm, cmbcallbackwm and lblcallback
			if ( isEPRule ) {
				if ( isProxy ) {
					cmbwm = cmbEPProxyWM;
					cmbcallbackwm = cmbEPProxyCallback;
					lblcallback = lblEPProxyCallback;
				} else {
					cmbwm = cmbEPWebMethod;
					cmbcallbackwm = cmbEPCallback;
					lblcallback = lblEPCallback;
				}
			} else {
				if ( isProxy ) {
					cmbwm = cmbProxyWebMethod;
					cmbcallbackwm = cmbProxyCallbackMethod;
					lblcallback = lblProxyCallback;
				} else {
					cmbwm = cmbWebMethod;
					cmbcallbackwm = cmbCallbackMethod;
					lblcallback = lblCallbackMethod;
				}
			}
			#endregion
			if ( wm != null && wm.OutputSchema == null ) {
				cmbcallbackwm.Enabled = cmbcallbackwm.Items.Count > 0;
				lblcallback.Enabled = cmbcallbackwm.Items.Count > 0;
			} else {
				cmbcallbackwm.Enabled = false;
				lblcallback.Enabled = false;
			}
			if ( wm == null ) {
				enableWMInputOutput(false, true, isProxy, isEPRule);
				enableWMInputOutput(false, false, isProxy, isEPRule);
				return;
			}
			// web service does not contains web method
			if ( !cmbwm.Items.Contains(wm) ) {
				if ( isEPRule ) {
					if ( lrre != null ) {
						if ( isProxy ) {
							lrre.ProxyWebMethodID = -1;
						} else {
							lrre.WebMethodID = -1;
						}
					}
				} else {
					if ( isProxy ) {
						_logwmeth.SetAssignedProxyWebMethod(null);
					} else {
						_logwmeth.SetAssignedWebMethod(null);
					}
				}
				enableWMInputOutput(false, true, isProxy, isEPRule);
				enableWMInputOutput(false, false, isProxy, isEPRule);
				return;
			}
			// display web method
			TextBox txtwmin, txtwmout;
			PictureBox picwmin, picwmout;
			XsltDocument xslt;
			if ( isEPRule ) {
				if ( isProxy ) {
					txtwmin = txtEPProxyIn;
					txtwmout = txtEPProxyOut;
					picwmin = picEPProxyInIsMapped;
					picwmout = picEPProxyOutIsMapped;
				} else {
					txtwmin = txtEPInput;
					txtwmout = txtEPOutput;
					picwmin = picEPInputIsMapped;
					picwmout = picEPOutputIsMapped;
				}
			} else {
				if ( isProxy ) {
					txtwmin = txtProxyWMInput;
					txtwmout = txtProxyWMOutput;
					picwmin = picProxyWMInputMapOK;
					picwmout = picProxyWMOutputMapOK;
				} else {
					txtwmin = txtWMInput;
					txtwmout = txtWMOutput;
					picwmin = picWMInputMapOK;
					picwmout = picWMOutputMapOK;
				}
			}
			if ( string.IsNullOrEmpty(wm.InputTypeName) ) {
				enableWMInputOutput(false, true, isProxy, isEPRule);
			} else {
				enableWMInputOutput(true, true, isProxy, isEPRule);
				txtwmin.Text = wm.InputTypeName;
				if ( isEPRule ) {
					if ( lrre != null ) {
						xslt = ( isProxy ? lrre.ProxyInputXSLT : lrre.InputXSLT );
					} else {
						xslt = null;
					}
				} else {
					xslt = ( isProxy ? _logwmeth.ProxyInputXSLT : _logwmeth.InputXSLT );
				}
				picwmin.Image = ( xslt == null ? Resources.png_cancel : Resources.png_ok );
			}
			if ( string.IsNullOrEmpty(wm.OutputTypeName) ) {
				if ( wmCallback != null && !string.IsNullOrEmpty(wmCallback.OutputTypeName) ) {
					enableWMInputOutput(true, false, isProxy, isEPRule);
					txtwmout.Text = wmCallback.OutputTypeName;
					if ( isEPRule ) {
						if ( lrre != null ) {
							xslt = ( isProxy ? lrre.ProxyOutputXSLT : lrre.OutputXSLT );
						} else {
							xslt = null;
						}
					} else {
						xslt = ( isProxy ? _logwmeth.ProxyOutputXSLT : _logwmeth.OutputXSLT );
					}
					picwmout.Image = ( xslt == null ? Resources.png_cancel : Resources.png_ok );
				} else {
					enableWMInputOutput(false, false, isProxy, isEPRule);
				}
			} else {
				enableWMInputOutput(true, false, isProxy, isEPRule);
				txtwmout.Text = wm.OutputTypeName;
				if ( isEPRule ) {
					if ( lrre != null ) {
						xslt = ( isProxy ? lrre.ProxyOutputXSLT : lrre.OutputXSLT );
					} else {
						xslt = null;
					}
				} else {
					xslt = ( isProxy ? _logwmeth.ProxyOutputXSLT : _logwmeth.OutputXSLT );
				}
				picwmout.Image = ( xslt == null ? Resources.png_cancel : Resources.png_ok );
			}
		}

		private void showWebServiceMethod(bool isProxy, bool isEPRule)
		{
			this.SuspendLayout();

			try {
				// get assigned web service
				WebService ws = null;
				WebMethod wm = null;
				WebMethod wmCallback = null;
				if ( isEPRule ) {
					#region get from current selected rule result EP
					LogicalRuleResultEndpoint lrre = getCurrentSelectedResultEP();
					if ( lrre != null ) {
						if ( isProxy ) {
							ws = _wsEPProxy;
							wm = lrre.ProxyWebMethod;
							wmCallback = lrre.ProxyCallbackWebMethod;
						} else {
							ws = _wsEP;
							wm = lrre.WebMethod;
							wmCallback = lrre.CallbackWebMethod;
						}
					}
					#endregion
				} else {
					#region get from assigned web service method
					if ( isProxy ) {
						ws = _wsProxy;
						wm = _logwmeth.GetAssignedProxyWebMethod();
						wmCallback = _logwmeth.GetProxyCallbackWebMethod();
					} else {
						ws = _logwmeth.GetAssignedWebService();
						wm = _logwmeth.GetAssignedWebMethod();
						wmCallback = _logwmeth.GetCallbackWebMethod();
					}
					#endregion
				}
				if ( ws == null && wm != null ) {
					#region get web service by repository hierarchy
					DataTable dt = Global.Data.GetRepositoryParentsOfType(ItemType.WebMethod, wm.ID, ItemType.WebService);
					if ( dt != null && dt.Rows.Count > 0 ) {
						foreach ( DataRow row in dt.Rows ) {
							if ( (ItemType)row["parent_reptype_id"] == ItemType.WebService ) {
								ws = Global.Data.RepositoryService.GetItem<WebService>((int)row["parent_id"]);
								break;
							}
						}
					}
					#endregion
				}
				if ( ws == null ) {
					#region currently no web service method is selected
					if ( isEPRule ) {
						if ( isProxy ) {
							txtEPProxyWS.Text = "Please select a proxy web service";
							txtEPProxyWS.TextAlign = HorizontalAlignment.Center;
							lblEPProxyURL.Text = "";
							txtEPProxyIn.Text = "";
							txtEPProxyOut.Text = "";
						} else {
							txtEPWebService.Text = "Please select a web service";
							txtEPWebService.TextAlign = HorizontalAlignment.Center;
							lblEPWebServiceURL.Text = "";
							txtEPInput.Text = "";
							txtEPOutput.Text = "";
						}
					} else {
						if ( isProxy ) {
							txtProxyWebService.Text = "Please select a proxy web service";
							txtProxyWebService.TextAlign = HorizontalAlignment.Center;
							lblProxyWebServiceURL.Text = "";
							txtProxyWMInput.Text = "";
							txtProxyWMOutput.Text = "";
						} else {
							txtWebService.Text = "Please select a web service";
							txtWebService.TextAlign = HorizontalAlignment.Center;
							lblWSUrl.Text = "";
							txtWMInput.Text = "";
							txtWMOutput.Text = "";
						}
					}
					enableWebMethod(false, isProxy, isEPRule);
					enableWMInputOutput(false, true, isProxy, isEPRule);
					enableWMInputOutput(false, false, isProxy, isEPRule);
					#endregion
					return;
				}
				#region display web service
				if ( isEPRule ) {
					if ( isProxy ) {
						txtEPProxyWS.Text = ws.Name;
						txtEPProxyWS.TextAlign = HorizontalAlignment.Left;
						lblEPProxyURL.Text = ws.Uri.ToString();
					} else {
						txtEPWebService.Text = ws.Name;
						txtEPWebService.TextAlign = HorizontalAlignment.Left;
						lblEPWebServiceURL.Text = ws.Uri.ToString();
					}
				} else {
					if ( isProxy ) {
						txtProxyWebService.Text = ws.Name;
						txtProxyWebService.TextAlign = HorizontalAlignment.Left;
						lblProxyWebServiceURL.Text = ws.Uri.ToString();
					} else {
						txtWebService.Text = ws.Name;
						txtWebService.TextAlign = HorizontalAlignment.Left;
						lblWSUrl.Text = ws.Uri.ToString();
					}
				}
				#endregion
				// fill web methods combobox
				enableWebMethod(true, isProxy, isEPRule);
				ComboBox cmbwm, cmbcallbackwm;
				Label lblcallback;
				#region set cmbwm, cmbcallbackwm and lblcallback
				if ( isEPRule ) {
					if ( isProxy ) {
						cmbwm = cmbEPProxyWM;
						cmbcallbackwm = cmbEPProxyCallback;
						lblcallback = lblEPProxyCallback;
					} else {
						cmbwm = cmbEPWebMethod;
						cmbcallbackwm = cmbEPCallback;
						lblcallback = lblEPCallback;
					}
				} else {
					if ( isProxy ) {
						cmbwm = cmbProxyWebMethod;
						cmbcallbackwm = cmbProxyCallbackMethod;
						lblcallback = lblProxyCallback;
					} else {
						cmbwm = cmbWebMethod;
						cmbcallbackwm = cmbCallbackMethod;
						lblcallback = lblCallbackMethod;
					}
				}
				#endregion
				// begin update
				cmbwm.BeginUpdate();
				cmbcallbackwm.BeginUpdate();
				// clear comboboxes
				cmbwm.Items.Clear();
				cmbcallbackwm.Items.Clear();
				// fill combo boxes
				BindingListEx<WebMethod> lstWMs = ws.GetWebMethods();
				foreach ( WebMethod wmCurrent in lstWMs ) {
					if ( wmCurrent.InputSchema == null ) {
						cmbcallbackwm.Items.Add(wmCurrent);
					}
				}
				cmbwm.Items.AddRange(lstWMs.ToArray());
				cmbwm.SelectedItem = wm;
				cmbcallbackwm.SelectedItem = wmCallback;
				// end update
				cmbwm.EndUpdate();
				cmbcallbackwm.EndUpdate();
				//	if ( cmbcallbackwm.Items.Count == 0 ) cmbcallbackwm.Enabled = false;
				if ( wm != null && wm.OutputSchema == null ) {
					cmbcallbackwm.Enabled = cmbcallbackwm.Items.Count > 0;
					lblcallback.Enabled = cmbcallbackwm.Items.Count > 0;
				} else {
					cmbcallbackwm.Enabled = false;
					lblcallback.Enabled = false;
				}
			} finally {
				this.ResumeLayout(true);
			}
		}

		#endregion Methods

		#endregion Private Members
	}
}